package demo.miner;

import jtech.Drawer;
import jtech.math.Vec2f;
import demo.miner.ClassBuilding.Grid;

public class EntityBuilding extends Entity<ClassBuilding> {

	public EntityBuilding(ClassBuilding classBuilding) {
		super(classBuilding);
	}

	@Override
	public void drawLifeBar(Game game, Drawer drawer) {
        if (isSelected() || isMouseOver()) {
        	Vec2f pos = game.toPointScreen(position);
            
        	float x = pos.x;
            float y = pos.y - (Game.HEALTH_BAR_HEIGHT * 3);
            float lifeBarWidth = getSpriteWidth();
            
            if (getHitPoints() > getBaseHitPoints() * .4f) {
            	drawer.color(Game.HEALTH_BAR_HEALTHY_FILL_COLOR);
            } else {
            	drawer.color(Game.HEALTH_BAR_DAMAGED_FILL_COLOR);
            }
            
            drawer.rectf(x, y, (lifeBarWidth * getHitPoints() / getBaseHitPoints()), Game.HEALTH_BAR_HEIGHT);
            
            drawer.color(Game.HEALTH_BAR_BORDER_COLOR);
            drawer.rect(x, y, lifeBarWidth, Game.HEALTH_BAR_HEIGHT);
        }
    }

	@Override
    public void drawSelection(Game game, Drawer drawer) {
		
		Vec2f pos = game.toPointScreen(position);
		float w = getSpriteWidth() / 2f;
		float h = getSpriteHeight() / 2f;
		
		drawer.lineWidth2();
		
    	if (isSelected()) {
			drawer.color(Game.SELECTION_BORDER_COLOR);
			drawer.circle(pos.x+w, pos.y+h, getBaseRadius() + 2);
		}
    	
		if (isSelectionOver() || isMouseOver()) {
			drawer.color(Game.DRAG_BORDER_COLOR);
			drawer.circle(pos.x+w, pos.y+h, getBaseRadius() + 3);
		}

    }
	
	@Override
    public  void drawImage(Game game, Drawer drawer) {
		Vec2f pos = game.toPointScreen(position);
		int w = getSpriteWidth();
		int h = getSpriteHeight();
		int dx = Math.round(pos.x + getSpriteOffsetX());
		int dy = Math.round(pos.y + getSpriteOffsetY());
		int sx = 0;
		int sy = 0;
		drawer.draw(getSpriteImage(), dx, dy, sx, sy, w, h);
	}
	
	@Override
	public void drawDebug(Game game, Drawer drawer) {
		Vec2f pos = game.toPointScreen(position);
		Vec2f posGrid = game.toMapGrid(position);
		int w = getSpriteWidth();
		int h = getSpriteHeight();
		int x = Math.round(pos.x);
		int y = Math.round(pos.y);
		drawer.red().circlef(x, y, 3);
		drawer.printc(x, y - 5, "%d,%d", (int) posGrid.x, (int) posGrid.y);
		drawer.rect(x, y, w, h);
		

		int[][] grid = getPassableGrid().getValue();
		
		for (int i = 0; i < grid.length; i++) {
			for (int j = 0; j < grid[i].length; j++) {
				int nx = x + (j * Game.GRID_SIZE);
				int ny = y + (i * Game.GRID_SIZE);
	            if (grid[i][j] == 1) {
	                drawer.color(255, 0, 0, 48);
	            } else {
	            	drawer.color(0, 255, 0, 48);
	            }
	            drawer.rectf(nx, ny, Game.GRID_SIZE, Game.GRID_SIZE);
				
			}
		}
	}
	
	/** Delegate methods */
	public Grid getPassableGrid() {
		return getClassEntity().getPassableGrid();
	}
	
	public Grid getBuildableGrid() {
		return getClassEntity().getBuildableGrid();
	}
	
	public int getBaseWidth() {
		return getClassEntity().getBaseWidth();
	}
	
	public int getBaseHeight() {
		return getClassEntity().getBaseHeight();
	}
	
}
